#include "qwidgethost.h"


#include "qabstractproperty.h"
#include "qpropertyfactory.h"
#include "qcommonstruct.h"
#include "qrenamedialog.h"

#include <QMetaMethod>
#include <QMouseEvent>
#include <QApplication>
#include <QAction>

using namespace SHAREDLIB;

QWidgetHost::QWidgetHost(QObject *object):
    QAbstractHost(object)
{
    initProperty();

    QAction* ac=new QAction(QIcon(),tr("Edit"),NULL);
    addAction("edit",ac);
    connect(ac,SIGNAL(triggered()),this,SLOT(edit_slots()));
}

void QWidgetHost::initProperty()
{
    QAbstractProperty *pro;
    pro=QPropertyFactory::createProperty("Name");
    if(pro!=NULL)
    {
        pro->setAttribute("name","objectName");
        pro->setAttribute("property_name",tr("Name"));
        pro->setAttribute("group","Attributes");
        pro->setAttribute("NeedSave",true);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Rect");
    if(pro!=NULL)
    {
        pro->setAttribute("name","geometry");
        pro->setAttribute("property_name",tr("Geometry"));
        pro->setAttribute("group","Attributes");
        pro->setAttribute("NeedSave",true);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Memery");
    if(pro!=NULL)
    {
        pro->setAttribute("name","memery");
        pro->setAttribute("property_name",tr("Memery"));
        pro->setAttribute("group","Attributes");
        pro->setAttribute("NeedSave",false);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Bool");
    if(pro!=NULL)
    {
        pro->setAttribute("name","enabled");
        pro->setAttribute("property_name",tr("Enabled"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Font");
    if(pro!=NULL)
    {
        pro->setAttribute("name","font");
        pro->setAttribute("property_name",tr("Font"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Cursor");
    if(pro!=NULL)
    {
        pro->setAttribute("name","cursor");
        pro->setAttribute("property_name",tr("Cursor"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Enum");
    if(pro!=NULL)
    {
        pro->setAttribute("name","focusPolicy");
        pro->setAttribute("property_name",tr("FocusPolicy"));
        pro->setAttribute("group","Attributes");
        ComboItems items;
        tagComboItem item;
        item.m_text="TabFocus";
        item.m_value=Qt::TabFocus;
        items.append(item);
        item.m_text="ClickFocus";
        item.m_value=Qt::ClickFocus;
        items.append(item);
        item.m_text="StrongFocus";
        item.m_value=Qt::StrongFocus;
        items.append(item);
        item.m_text="WheelFocus";
        item.m_value=Qt::WheelFocus;
        items.append(item);
        item.m_text="NoFocus";
        item.m_value=Qt::NoFocus;
        items.append(item);
        QVariant v;
        v.setValue<ComboItems>(items);
        pro->setAttribute("items",v);
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("String");
    if(pro!=NULL)
    {
        pro->setAttribute("name","toolTip");
        pro->setAttribute("property_name",tr("ToolTip"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","mousePress");
        pro->setAttribute("property_name",tr("MousePress"));
        pro->setAttribute("group","Events");
        pro->setAttribute("param",QStringList()<<"param_x"<<"param_y"<<"param_button");
        pro->setAttribute("method_tips",tr("[x,y] is the mouse position.\n[button] is the mouse button type {0:nobutton 1:leftbutton 2:rightbutton 3:middlebutton}."));
        m_object->setProperty("mousePress","");
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","mouseMove");
        pro->setAttribute("property_name",tr("MouseMove"));
        pro->setAttribute("group","Events");
        pro->setAttribute("param",QStringList()<<"param_x"<<"param_y"<<"param_button");
        pro->setAttribute("method_tips",tr("[x,y] is the mouse position.\n[button] is the mouse button type {0:nobutton 1:leftbutton 2:rightbutton 3:middlebutton}."));
        m_object->setProperty("mouseMove","");
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","mouseRelease");
        pro->setAttribute("property_name",tr("MouseRelease"));
        pro->setAttribute("group","Events");
        pro->setAttribute("param",QStringList()<<"param_x"<<"param_y"<<"param_button");
        pro->setAttribute("method_tips",tr("[x,y] is the mouse position.\n[button] is the mouse button type {0:nobutton 1:leftbutton 2:rightbutton 3:middlebutton}."));
        m_object->setProperty("mouseRelease","");
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","doubleClick");
        pro->setAttribute("property_name",tr("DoubleClick"));
        pro->setAttribute("group","Events");
        pro->setAttribute("param",QStringList()<<"param_x"<<"param_y"<<"param_button");
        pro->setAttribute("method_tips",tr("[x,y] is the mouse position.\n[button] is the mouse button type {0:nobutton 1:leftbutton 2:rightbutton 3:middlebutton}."));
        m_object->setProperty("doubleClick","");
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("Stylesheet");
    if(pro!=NULL)
    {
        pro->setAttribute("name","background");
        pro->setAttribute("property_name",tr("Background"));
        pro->setAttribute("group","Style Sheet");

        StyleItems items;
        tagStyleItem item;

        item.m_string="normal";
        item.m_text=tr("Normal");
        items.append(item);


        item.m_string="hover";
        item.m_text=tr("Hover");
        items.append(item);

        QVariant v;
        v.setValue<StyleItems>(items);
        pro->setAttribute("items",v);

        m_object->setProperty("background",QVariant());
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("Stylesheet");
    if(pro!=NULL)
    {
        pro->setAttribute("name","border");
        pro->setAttribute("property_name",tr("Border"));
        pro->setAttribute("group","Style Sheet");

        StyleItems items;
        tagStyleItem item;

        item.m_string="normal";
        item.m_text=tr("Normal");
        items.append(item);


        item.m_string="hover";
        item.m_text=tr("Hover");
        items.append(item);

        QVariant v;
        v.setValue<StyleItems>(items);
        pro->setAttribute("items",v);

        m_object->setProperty("background",QVariant());
        addProperty(pro);
    }

    this->appendMemery("toolTip");
}

void QWidgetHost::appendMemery(const QString &property)
{
    QStringList list=m_object->property("memeryList").toStringList();
    list.append(property);
    m_object->setProperty("memeryList",list);
}

void QWidgetHost::setGeometry(int x, int y, int width, int height)
{
    QRect rect(x,y,width,height);
    setPropertyValue("geometry",rect);
}

void QWidgetHost::setEnabled(bool enabled)
{
    setPropertyValue("enabled",enabled);
}

void QWidgetHost::setToolTip(const QString &tooltip)
{
    setPropertyValue("toolTip",tooltip);
}

int QWidgetHost::x()
{
    return getPropertyValue("geometry").toRect().x();
}

int QWidgetHost::y()
{
    return getPropertyValue("geometry").toRect().y();
}

int QWidgetHost::width()
{
    return getPropertyValue("geometry").toRect().width();
}

int QWidgetHost::height()
{
    return getPropertyValue("geometry").toRect().height();
}

bool QWidgetHost::enabled()
{
    return getPropertyValue("enabled").toBool();
}

QString QWidgetHost::toolTip()
{
    return getPropertyValue("toolTip").toString();
}

void QWidgetHost::setVisible(bool visible)
{
    m_object->setProperty("visible",visible);
}

bool QWidgetHost::visible()
{
    return m_object->property("visible").toBool();
}

void QWidgetHost::setCursor(int cursor)
{
    QCursor c((Qt::CursorShape)cursor);
    QVariant v;
    v.setValue<QCursor>(c);
    setPropertyValue("cursor",v);
}

int QWidgetHost::cursor()
{
    QCursor c=getPropertyValue("cursor").value<QCursor>();
    return c.shape();
}

void QWidgetHost::mouseMoveEvent(QMouseEvent *e)
{
    QString code=getPropertyValue("mouseMove").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("param_x",QString::number(e->x()));
        param.insert("param_y",QString::number(e->y()));
        param.insert("param_button",QString::number(e->button()));
        emit_exec(code,param);
    }
}

void QWidgetHost::pressEvent(QMouseEvent *e)
{
    QString code=getPropertyValue("mousePress").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("param_x",QString::number(e->x()));
        param.insert("param_y",QString::number(e->y()));
        param.insert("param_button",QString::number(e->button()));
        emit_exec(code,param);
    }
}

void QWidgetHost::releaseEvent(QMouseEvent *e)
{
    QString code=getPropertyValue("mouseRelease").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("param_x",QString::number(e->x()));
        param.insert("param_y",QString::number(e->y()));
        param.insert("param_button",QString::number(e->button()));
        emit_exec(code,param);
    }
}

void QWidgetHost::doubleClickEvent(QMouseEvent *e)
{
    QString code=getPropertyValue("doubleClick").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("param_x",QString::number(e->x()));
        param.insert("param_y",QString::number(e->y()));
        param.insert("param_button",QString::number(e->button()));
        emit_exec(code,param);
    }
}

bool QWidgetHost::handleEvent(QEvent *e)
{
    switch(e->type())
    {
    case QEvent::MouseMove:
        mouseMoveEvent((QMouseEvent*)e);
        break;
    case QEvent::MouseButtonPress:
        pressEvent((QMouseEvent*)e);
        break;
    case QEvent::MouseButtonRelease:
        releaseEvent((QMouseEvent*)e);
        break;
    case QEvent::MouseButtonDblClick:
        doubleClickEvent((QMouseEvent*)e);
        break;
    default:
        break;
    }

    return QAbstractHost::handleEvent(e);
}

void QWidgetHost::edit_slots()
{
    QRenameDialog dialog((QWidget*)m_object);

    dialog.set_old_name(m_object->property("objectName").toString());

    QStringList list;
    if(m_parent!=NULL)
    {
        foreach(QAbstractHost *h,m_parent->getChildren())
        {
            if(h!=this)
            {
                list.append(h->getPropertyValue("objectName").toString());
            }
        }
    }
    dialog.set_name_list(list);

    dialog.exec();

    if(dialog.get_ret()==1)
    {
        m_object->setObjectName(dialog.get_new_name());
    }
}
